package com.formation.tocea.library.client.ui.list;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.CellPreviewEvent;
import com.google.gwt.view.client.CellPreviewEvent.Handler;
import com.google.gwt.view.client.ListDataProvider;

public class BookTable extends CellTable<BookItem> {
	public static List<BookItem> BOOKS = Arrays.asList(new BookItem(1, "1984", "Orwell", "1234567897458", "Utopia", "2000"),
														new BookItem(2, "Crime and Punishment", "Dostoievskii", "4534667897458", "Classic", "2001"));
	private List<BookItem> bookList;
	
	public BookTable() {
		init();
	}
	
	public void init() {
		setWidth("100%");
	    // Create title column.
	    TextColumn<BookItem> titleColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	        return book.getTitle();
	      }
	    };

	    // Create author column.
	    TextColumn<BookItem> authorColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	        return book.getAuthor();
	      }
	    };
	    
	    // Create isbn column.
	    TextColumn<BookItem> isbnColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	        return book.getIsbn();
	      }
	    };

	    // Create genre column.
	    TextColumn<BookItem> genreColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	        return book.getGenre();
	      }
	    };
	    
	    // Create edition date column.
	    TextColumn<BookItem> editionDateColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	        return book.getEditionDate();
	      }
	    };
	    
	    // Create edition date column.
	    TextColumn<BookItem> nbCopiesColumn = new TextColumn<BookItem>() {
	      public String getValue(BookItem book) {
	    	  if (book.getNbCopies() == null) {
	    		  return "0";
	    	  }
	        return String.valueOf(book.getNbCopies());
	      }
	    };
	    
	    Cell<Boolean> removeCell = new CheckboxCell();
	    Column<BookItem, Boolean> removeColumn = new Column<BookItem, Boolean>(removeCell) {
			
			@Override
			public Boolean getValue(BookItem book) {
				return book.getRemove();
			}
		};
	    
	    // Add the columns.
		addColumn(removeColumn, "Supprimer");
	    addColumn(titleColumn, 	"Title");
	    addColumn(authorColumn, "Author");
	    addColumn(isbnColumn, 	"ISBN");
	    addColumn(genreColumn, 	"Genre");
	    addColumn(editionDateColumn, 	"Date édition");
	    addColumn(nbCopiesColumn, 		"Nombre d'exemplaires");
	    
	    addCellPreviewHandler(new Handler<BookItem>() {
			public void onCellPreview(CellPreviewEvent<BookItem> event) {
				if (event.getNativeEvent().getType().equals("click")) {
					System.out.println("-->"+event.getValue().getTitle());					
				}
			}
		});
	    
	    // Create a data provider.
	    ListDataProvider<BookItem> dataProvider = new ListDataProvider<BookItem>();

	    // Connect the table to the data provider.
	    dataProvider.addDataDisplay(this);

	    // Add the data to the data provider, which automatically pushes it to the
	    // widget.
	    bookList = dataProvider.getList();
	    for (BookItem book : BOOKS) {
	      bookList.add(book);
	    }
	}

	public List<BookItem> getBookList() {
		return bookList;
	}
}
